package com.huoyun.order.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Page;
import com.huoyun.car.bean.Driver;
import com.huoyun.car.service.CarService;
import com.huoyun.car.service.DriverService;
import com.huoyun.order.bean.Order;
import com.huoyun.order.bean.Rate;
import com.huoyun.order.service.OrderService;
import com.huoyun.order.service.RateService;
import com.huoyun.order.service.evt.GetRateByCarIdEvt;
import com.huoyun.order.service.evt.UpdateOrderStatusEvt;
import com.huoyun.user.bean.User;
import com.huoyun.user.service.UserService;

public class RateAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private OrderService orderService;

	private UserService userService;

	private RateService rateService;

	private CarService carService;

	private DriverService driverService;

	// 进入评价页面前，获取对应订单信息
	private Order orderObj;

	// 用于保存评价信息的对象
	private Rate rate = new Rate();

	// 页面传递过来评价相关的orderId，用于获取评价前order相关的信息
	private Long orderId;

	private int priceLevel_l = 5000;
	private int priceLevel_m = 10000;

	private String itemArray[] = { "服务态度：", "车源信息准确性：", "运输及时性：", "货物完整性：" };
	private List<String> itemListForCar = Arrays.asList(itemArray);
	private List<String> rateList = new ArrayList<String>();

	private long carId;
	private Page ratePage;

	/**
	 * 用于进入评价页面时读取相关的订单信息
	 */
	public String showOrder() {
		try {
			orderObj = orderService.getOrderById(orderId);
		} catch (Exception e) {

		}
		return SUCCESS;
	}

	/**
	 * 插入新的评价，更改订单状态为4，更新用户积分
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveRate() throws Exception {
		long userId = this.getLoginUser().getId();
		rate.setAuthorId(userId);
		try {
			Order order = orderService.getOrderById(rate.getOrderId());
			// 根据登陆用户判断该评价的接收者id，如果对车主评价，则需要添加司机id
			long driverId = carService.getCarDriverOwnerModelByCarId(order.getCarId()).getDriver().getId();
			if (order.getCarOwnerId() == userId) {
				rate.setReceiverId(order.getCargosOwnerId());
			} else {
				rate.setReceiverId(order.getCarOwnerId());
				rate.setDriverId(driverId);
			}
			// 构造星级评分字符串
			StringBuilder rateStars = new StringBuilder();
			for (int i = 0; i < rateList.size() - 1; i++) {
				rateStars.append(rateList.get(i));
				rateStars.append("/");
			}
			rateStars.append(rateList.get(rateList.size() - 1));
			rate.setRateItem(rateStars.toString());
			// 人为评价
			rate.setStatus(2);

			// 更新用户积分
			User user = userService.getUserById(rate.getReceiverId());
			long credit = user.getCredit();
			if (Integer.parseInt(order.getPrice()) < priceLevel_l) {
				credit += rate.getRateTotal();
			} else if (Integer.parseInt(order.getPrice()) >= priceLevel_l && Integer.parseInt(order.getPrice()) < priceLevel_m) {
				credit += rate.getRateTotal() * 2;
			} else {
				credit += rate.getRateTotal() * 3;
			}
			userService.updateCredit(credit, rate.getReceiverId());
			// 插入新的评价
			rateService.saveRate(rate);

			// 更新订单状态为4，即已评价
			UpdateOrderStatusEvt updateOrderStatusEvt = new UpdateOrderStatusEvt();
			updateOrderStatusEvt.setStatus(4);
			updateOrderStatusEvt.setOrderId(rate.getOrderId());
			orderService.updateOrderStatus(updateOrderStatusEvt);

			// 更新司机的动态评分
			if (order.getCarOwnerId() != userId) {
				Driver driver = driverService.getDriverById(driverId);
				String rateDb = driver.getRate();
				String[] rateDbItem = rateDb.split("/");
				for (int i = 0; i < rateDbItem.length; i++) {
					rateDbItem[i] = (Integer.parseInt(rateDbItem[i].trim()) + Integer.parseInt(rateList.get(i).trim())) + "";
				}
				// 构造星级评分字符串
				StringBuilder rateStr = new StringBuilder();
				for (int i = 0; i < rateDbItem.length - 1; i++) {
					rateStr.append(rateDbItem[i]);
					rateStr.append("/");
				}
				rateStr.append(rateDbItem[rateDbItem.length - 1]);
				driverService.updateRate(rateStr.toString(), driver.getCount() + 1, driverId);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 获取某辆车的评价信息
	 * 
	 * @return
	 */
	public String rateList() {
		try {
			GetRateByCarIdEvt evt = new GetRateByCarIdEvt();
			evt.setCarId(carId);
			evt.setPageNo(pageNo);
			evt.setPageCount(PAGE_COUNT);

			ratePage = rateService.queryRate(evt);
		} catch (Exception e) {
			e.fillInStackTrace();
		}
		return SUCCESS;
	}

	public Page getRatePage() {
		return ratePage;
	}

	public void setRatePage(Page ratePage) {
		this.ratePage = ratePage;
	}

	public long getCarId() {
		return carId;
	}

	public void setCarId(long carId) {
		this.carId = carId;
	}

	public List<String> getRateList() {
		return rateList;
	}

	public void setRateList(List<String> rateList) {
		this.rateList = rateList;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public Order getOrderObj() {
		return orderObj;
	}

	public void setOrderObj(Order orderObj) {
		this.orderObj = orderObj;
	}

	public Long getOrderId() {
		return orderId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public DriverService getDriverService() {
		return driverService;
	}

	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}

	public RateService getRateService() {
		return rateService;
	}

	public void setRateService(RateService rateService) {
		this.rateService = rateService;
	}

	public List<String> getItemListForCar() {
		return itemListForCar;
	}

	public void setItemListForCar(List<String> itemListForCar) {
		this.itemListForCar = itemListForCar;
	}

	public Rate getRate() {
		return rate;
	}

	public void setRate(Rate rate) {
		this.rate = rate;
	}

}
